<template>
  <div class="datagrid-wrapper">
    <div class="datagrid-toolbar">
      <slot name="toolbar"></slot>
    </div>
    <div class="datagrid" style="overflow: auto;">
      <slot></slot>
    </div>
    <div class="pagination-wrapper">
      <el-pagination style="margin-right: 14px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.page"
          :page-sizes="pageSizes"
          :page-size="queryParams.rows"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="queryParams.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { inject } from 'vue'

export default {
  name: 'datagridCard',
  data () {
    return {}
  },
  props: {
    pageSizes: {
      type: Array,
      default: () => {
        return [10, 20, 50, 100]
      }
    }
  },
  methods: {},
  setup () {
    const queryParams = inject('queryParams')
    const updateQueryParams = inject('updateQueryParams')
    const handleSizeChange = (val) => {
      updateQueryParams({ rows: val })
    }
    const handleCurrentChange = (val) => {
      updateQueryParams({ page: val })
    }
    const handlePrevClick = (val) => {
      updateQueryParams({ page: val })
    }
    const handleNextClick = (val) => {
      updateQueryParams({ page: val })
    }
    return { queryParams, handleSizeChange, handleCurrentChange, handlePrevClick, handleNextClick }
  }
}
</script>

<style lang="scss" scoped>
.datagrid-wrapper {
  height: 100%;
  position: relative;
  overflow: hidden;
  border: 1px solid #dcdfe6;
  box-sizing: border-box;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);

  .datagrid-toolbar {
    position: absolute;
    width: 100%;
    height: 50px;
    top: 0;
    left: 0;
  }

  .datagrid {
    position: absolute;
    width: 100%;
    top: 50px;
    bottom: 50px;
  }

  .pagination-wrapper {
    position: absolute;
    width: 100%;
    height: 50px;
    bottom: 0;
    left: 0;
    display: flex;
    justify-content: flex-end;
    background-color: white;
    text-align: right;
    padding: 5px ;
  }
}
</style>
